﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;

namespace Catering.UCPanle
{
    public partial class UCRightSearchTool : System.Web.UI.UserControl
    {
        #region 属性
        FromRecords frRules = null;
        T_RawMaterials rawMaterialsRule = null;
        //List<CheckBox> listCheckBox = null;//存储页面所有CheckBox
        private string[] years;//年份
        private int[] RawId;//食品大类ID
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }
        }

        #region ListBox值兑换
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbUNSelect.SelectedItem != null)
                {
                    lbSelected.Items.Add(lbUNSelect.SelectedItem);
                    lbUNSelect.Items.Remove(lbUNSelect.SelectedItem);
                }
            }
            catch
            {

            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbSelected.SelectedItem != null)
                {
                    lbUNSelect.Items.Add(lbSelected.SelectedItem);
                    lbSelected.Items.Remove(lbSelected.SelectedItem);
                }
            }
            catch
            {

            }
        }
        #endregion

        /// <summary>
        /// 获取选中大类
        /// </summary>
        /// <returns></returns>
        public int[] GetRaw()
        {
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }

            RawId = new int[selectNum1];
            int numTemp = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawId[numTemp] = Int32.Parse(CheckBoxList1.Items[i].Value);
                    numTemp += 1;
                }
            }
            return RawId;
        }

        /// <summary>
        /// 获取时间
        /// </summary>
        /// <returns></returns>
        public string[] GetYears()
        {
            if (lbSelected.Items.Count > 0)
            {
                years = new string[lbSelected.Items.Count];
                for (int i = 0; i < lbSelected.Items.Count; i++)
                {
                    years[i] = lbSelected.Items[i].Value;
                }
            }
            return years;
        }

        private void loadData()
        {
            frRules = new FromRecords();
            rawMaterialsRule = new T_RawMaterials();
            List<string> listyears = frRules.GetYears();
            if (listyears != null && listyears.Count > 0)
            {
                Bind_Data_to_Controls.BindListBox(listyears, this.lbUNSelect);
            }
            List<Catering_Model.E_RawMaterials> listMaterials = rawMaterialsRule.GetAllList(0);
            Bind_Data_to_Controls.Bind_Material_cbo(listMaterials, this.CheckBoxList1);
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CheckBoxList1.SelectedItem.Text.Equals("全部"))
            {
                if (CheckBoxList1.SelectedItem.Selected)
                {
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        CheckBoxList1.Items[i].Selected = true;
                    }
                }
            }
            else
            {
                int num = 0;
                for (int i = 1; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected == true)
                    {
                        num = num + 1;
                    }
                }
                if (num == (CheckBoxList1.Items.Count - 1))
                {
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        CheckBoxList1.Items[i].Selected = false;
                    }
                }
            }



        }

        protected void UpdatePanel1_DataBinding(object sender, EventArgs e)
        {
        }
    }
}